package com.example.work12.utils;

import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;

import com.example.work12.bean.Music;

import java.util.ArrayList;
import java.util.List;

public class MusicUtils {

    public static List<Music> getMusic(Context context){
        List<Music> musics = new ArrayList<>();
        Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
        while (cursor.moveToNext()){
            String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
            String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
            String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
            long size = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE));
            long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));

            Music music = new Music(data, artist, name, size, duration);
            musics.add(music);
        }
        cursor.close();
        return musics;
    }

    public static String formatTime(long duration){
        if(duration / 1000 % 60 < 10){
            return duration /1000 /60 +":0" +duration/1000%60;
        }else {
            return duration /1000 /60 +":" +duration/1000%60;
        }
    }
}
